Euler Problem 49

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways:

- each of the three terms are prime, and,
- each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?


In [1]:
from sympy import isprime
from collections import defaultdict
from itertools import combinations

primes = defaultdict(list)
for n in range(1001, 10000, 2):
    if isprime(n):
        key = ''.join(sorted(str(n)))
        primes[key].append(n)
for key in primes:
    for p, q in combinations(primes[key], 2):
        r = q + (q - p)
        if r in primes[key]:
            print(f'{p}{q}{r}')
            break


148748178147
296962999629

In [ ]: